package com.example.basic.utils;


import com.example.basic.exception.ServiceException;

import java.util.List;

/**
 * 校验工具类
 * 
 * @author 肖文杰
 *
 */
public class CheckUtil {

	public static void check(boolean condition, String msgKey, Object... args) {
		if (!condition) {
			fail(msgKey, args);
		}
	}

	public static void notEmpty(String str, String msgKey, Object... args) {
		if (str == null || str.isEmpty()) {
			fail(msgKey, args);
		}
	}
	
	@SuppressWarnings("rawtypes")
	public static void notListEmpty(List lst, String msgKey, Object... args) {
		if (lst == null || lst.isEmpty()) {
			fail(msgKey, args);
		}
	}

	public static void notNull(Object obj, String msgKey, Object... args) {
		if (obj instanceof String) {
			notEmpty((String) obj,msgKey);
		}else if(obj instanceof List){
			notListEmpty((List) obj,msgKey);
		}else if(obj == null || obj == ""){
			fail(msgKey, args);
		}
	}

	private static void fail(String msgKey, Object... args) {
		 throw new ServiceException(msgKey);
	}
}
